<template>
<!-- 	<u-button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"> 一键登录 </u-button> -->
	<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"> 获取电话号码</button>
	<text>123123</text>
</template>

<script>
	// import WXBizDataCrypt from "../../common/WXBizDataCrypt";
	
	export default {
		
		data() {
			return{
				login_code:'',
				WX_AUTH_URL:'https://api.weixin.qq.com/sns/jscode2session',
				APPID:'wxd2bae8750b0ed5ef',
				SECRET:'fcc0f2314b00e054a87e8e38310091c1',
				
			}
		},
		methods: {
			getPhoneNumber: function(e) {
				// const WXBizDataCrypt = require('../../common/WXBizDataCrypt');
				console.log(e);
				var that = this
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
					console.log('用户拒绝提供手机号');
				} else {
					console.log('用户同意提供手机号');

					var encryptedData = e.detail.encryptedData;
					var iv = e.detail.iv;
					var JSCODE = this.login_code;
					var APPID = this.APPID;
					var SECRET = this.SECRET;
					var wx_author_url = this.WX_AUTH_URL + '?appid=' + APPID + '&secret=' + SECRET + '&js_code=' +
						JSCODE + '&grant_type=authorization_code';
						
					console.log(wx_author_url)

					uni.request({
						url: wx_author_url,
						success(re) {
							console.log('session_key:' + re.data.session_key);

							var appId = APPID;
							var sessionKey = re.data.session_key;
							console.log(appId,sessionKey,encryptedData,iv)

							var pc = new WXBizDataCrypt(appId, sessionKey);
							var data = pc.decryptData(encryptedData, iv);

							console.log('------------------->');
							console.log('解密后 data: ', data);
							// console.log('解密后 data: ', JSON.stringify(data));  
							console.log('------------------->');

						}
					});

				}

			}

		},

		// methods: {
		// 	getNumber(e) {
		// 		let appid = 'wxd2bae8750b0ed5ef'
		// 		let secret = 'fcc0f2314b00e054a87e8e38310091c1'
		// 		uni.request({
		// 			url: 'https://api.weixin.qq.com/cgi-bin/token',
		// 			method: 'GET',
		// 			data: {
		// 				appid: appid,
		// 				secret: secret,
		// 				grant_type: 'client_credential'
		// 			},
		// 			success: (res) => {
		// 				console.log(res.data.access_token)
		// 				let accessToken = res.data.access_token
		// 				uni.request({
		// 					url: `https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${accessToken}`,
		// 					method: 'POST',
		// 					data: {
		// 						code: e.detail.code
		// 					},
		// 					success: (result) => {

		// 						if (result.data.errcode === 0) {
		// 							let phone = result.data.phone_info.phoneNumber
		// 							console.log(phone)
		// 						}
		// 					},
		// 					fail() {
		// 						uni.showToast({
		// 							title: '获取失败,请重试1',
		// 							icon: 'error'
		// 						})
		// 					}
		// 				})
		// 			},
		// 			fail() {
		// 				uni.showToast({
		// 					title: '获取失败,请重试2',
		// 					icon: 'error'
		// 				})
		// 			}
		// 		})
		// 	}

		// },
		mounted() {
			var that = this;
			uni.login({
				success: function(res) {
					// 获取code    
					console.log(JSON.stringify(res));
					console.log("已登陆");
					that.login_code = res.code;
				}

			});
		}
	}
</script>　